package com.smile.job;

import com.alibaba.fastjson.JSONObject;
import com.smile.HeartBeatData;
import com.smile.api.OnSiteInspectionApi;
import com.smile.model.request.ExecuteOnSiteInspectionRequest;
import com.smile.mqtt.handler.AbstractDrcDownHandler;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class FeisuanJob {

    @Autowired
    private AbstractDrcDownHandler abstractDrcDownHandler;


    @Autowired
    private OnSiteInspectionApi onSiteInspectionApi;
    ;

    /**
     * 执行巡警
     *
     * @param param
     */
//    @XxlJob("heartbeat")
//    public void executeOnSiteInspection(String param) {
    public void heartbeat(String param) {
        System.out.println(XxlJobHelper.getJobParam());
//        System.out.println("测试定时任务");
        JSONObject jsonObject = new JSONObject();
        HeartBeatData heartBeatData = new HeartBeatData();
        heartBeatData.setTimestamp(System.currentTimeMillis());
        jsonObject.put("method", "heart_beat");
        System.out.println(jsonObject.toJSONString());
        jsonObject.put("data", heartBeatData);
        try {
            abstractDrcDownHandler.process(jsonObject.toJSONString());
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
    }

    @XxlJob("executeOnSiteInspection")
    public void executeOnSiteInspection(String param) {
        System.out.println(XxlJobHelper.getJobParam());
        ExecuteOnSiteInspectionRequest executeOnSiteInspectionRequest = new ExecuteOnSiteInspectionRequest();
        executeOnSiteInspectionRequest.setCode(param);
        onSiteInspectionApi.executeOnSiteInspection(executeOnSiteInspectionRequest);
    }

}
